#!/bin/bash
if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash build.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || "$1" = "/" ]] && echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || "$2" = "/" ]] && echo "请输入正确的安装路径" && exit 1

create_dir() {
    local path=$1
    [[ "$path" =~ .*/$ ]] && path=${path%/*}
    if [[ ! -d "$path" && ! -f "$path" ]]; then mkdir -p $path; else path=$path$(date "+%y%m%d%H%M%S") && mkdir -p $path; fi
    echo $path
}

buildpath=$(create_dir $1)
installpath=$(create_dir $2)

yum install -y wget m4 environment-modules systemd-devel hdf5 hdf5-devel bc

source /etc/profile
module purge

set -e
dep_dir="dependency"
bash $dep_dir/bisheng-compiler-2.1.0.sh $buildpath $installpath
module use $installpath/bisheng-compiler-2.1.0
module load $installpath/bisheng-compiler-2.1.0/bisheng_modulefiles

bash $dep_dir/hmpi-1.1.1.sh $buildpath $installpath
module use $installpath/hmpi-1.1.1
module load $installpath/hmpi-1.1.1/hmpi_modulefiles

yum -y install qt5-qtx11extras qt5-qtx11extras-devel cmake qt5-qtquickcontrols qt5-qtdeclarative-devel qt5-qtbase-devel
yum install git wget tcsh libgfortran-static make vim-common bc -y
yum install lapack-devel lapack-static blas-devel blas-static zlib-devel -y
yum install libX11-devel libXmu-devel mesa-libGL-devel freeglut-devel libXi-devel -y
yum install -y python3 python3-numpy libjpeg libtiff giflib gstreamer* libgtk* gtk2-devel libxvidcore* libx264* libgtk-3* gtk3-devel tbb openblas lapack jasper hdf5-devel protobuf
yum install python-devel python3-devel -y
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple h5py numpy


bash $dep_dir/petsc-2.3.3.sh $buildpath $installpath
module use $installpath/petsc
module load $installpath/petsc/petsc_modulefiles

bash $dep_dir/itk-4.13.3.sh $buildpath $installpath
module use $installpath/ITK
module load $installpath/ITK/itk_modulefiles

bash $dep_dir/sip-4.18.sh $buildpath $installpath

bash $dep_dir/pyqt.sh $buildpath $installpath

bash $dep_dir/ann-1.1.2.sh $buildpath $installpath
module use $installpath/ann_1.1.2
module load $installpath/ann_1.1.2/ann_modulefiles

bash $dep_dir/armadillo-9.200.7.sh $buildpath $installpath
module use $installpath/armadillo
module load $installpath/armadillo/armadillo_modulefiles

bash $dep_dir/VTK-7.1.1.sh $buildpath $installpath
module use $installpath/VTK
module load $installpath/VTK/vtk_modulefiles

bash $dep_dir/OpenCV-4.5.5.sh $buildpath $installpath
module use $installpath/opencv
module load $installpath/opencv/opencv_modulefiles

bash $dep_dir/git-annex.sh $buildpath $installpath

bash freesurfer.sh $buildpath $installpath
module use $installpath/freesurfer
module load $installpath/freesurfer/freesurfer_modulefiles
source $FREESURFER_HOME/SetUpFreeSurfer.sh

mkdir -p pkg/freesurfer-7.2-hpc && cp -r $installpath/* pkg/freesurfer-7.2-hpc/

cat >"pkg/freesurfer-7.2-hpc/run.sh" <<EOF
#!/bin/bash
yum install bc libatomic environment-modules wget cmake tar perl-Data-Dumper autoconf automake libtool numactl binutils systemd-devel valgrind flex gcc-c++ gcc-gfortran git -y
yum install -y tcsh libgfortran-static make vim-common lapack-devel lapack-static blas-devel blas-static zlib-devel python-devel python3-devel libX11-devel libXmu-devel mesa-libGL-devel freeglut-devel libXi-devel
yum install qt5-qtquickcontrols qt5-qtdeclarative-devel qt5-qtbase-devel -y
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple h5py
source /etc/profile &> /dev/null 
current_dir=\$(cd "\$(dirname "\${BASH_SOURCE[0]}")" &> /dev/null && pwd)
for d in \$(ls \$current_dir)
do
    [[ ! -f "\$current_dir/\$d" ]] && cd \$current_dir/\$d && module use \$(pwd) && module load \$(pwd)/*modulefiles
done
source \$FREESURFER_HOME/SetUpFreeSurfer.sh
cd \$current_dir
[[ ! -d "$installpath/hmpi-1.1.1" ]] && mkdir -p $installpath/hmpi-1.1.1 && cp -r \$current_dir/hmpi-1.1.1/* $installpath/hmpi-1.1.1/
echo "请到http://surfer.nmr.mgh.harvard.edu/registration.html进行注册下载license并编辑放到freesurfer的.license文件里"
EOF
cd pkg/ && tar zcvf freesurfer-7.2-hpc.tar.gz freesurfer-7.2-hpc

rm -rf $buildpath $installpath
